using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The EnquiryTypeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about EnquiryTypeTable objects.
    /// </summary>
    public class EnquiryTypeTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of EnquiryTypeTable from the underlying datasource.
        /// </summary>
        /// <param name="enquiryType">The unique EnquiryType of the EnquiryTypeTable in the database.</param>
        /// <returns>An EnquiryTypeTable when the EnquiryType was found in the database, or null otherwise.</returns>
        public static EnquiryTypeTable GetItem(short enquiryType)
        {
            EnquiryTypeTable myEnquiryTypeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEnquiryTypeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@enquiryType", enquiryType);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myEnquiryTypeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myEnquiryTypeTable;
            }
        }

        /// <summary>
        /// Returns a list with EnquiryTypeTable objects.
        /// </summary>
        /// <returns>A generics List with the EnquiryTypeTable objects.</returns>
        public static EnquiryTypeTableList GetList()
        {
            EnquiryTypeTableList tempList = new EnquiryTypeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEnquiryTypeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a EnquiryTypeTable in the database.
        /// </summary>
        /// <param name="myEnquiryTypeTable">The EnquiryTypeTable instance to save.</param>
        /// <returns>The new EnquiryType if the EnquiryTypeTable is new in the database or the existing EnquiryType when an item was updated.</returns>
        public static Int16 Save(EnquiryTypeTable myEnquiryTypeTable)
        {
            Int16 result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEnquiryTypeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myEnquiryTypeTable.EnquiryType == -1)
                    {
                        myCommand.Parameters.AddWithValue("@enquiryType", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@enquiryType", myEnquiryTypeTable.EnquiryType);
                    }
                    if (string.IsNullOrEmpty(myEnquiryTypeTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myEnquiryTypeTable.Description);
                    }
                    if (myEnquiryTypeTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myEnquiryTypeTable.Status);
                    }
                    if (myEnquiryTypeTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myEnquiryTypeTable.CreatedDateTime);
                    }
                    if (myEnquiryTypeTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myEnquiryTypeTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt16(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a EnquiryTypeTable from the database.
        /// </summary>
        /// <param name="enquiryType">The EnquiryType of the EnquiryTypeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(short enquiryType)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocEnquiryTypeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@enquiryType", enquiryType);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the EnquiryTypeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static EnquiryTypeTable FillDataRecord(IDataRecord myDataRecord)
        {
            EnquiryTypeTable myEnquiryTypeTable = new EnquiryTypeTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EnquiryType")))
            {
                myEnquiryTypeTable.EnquiryType = myDataRecord.GetInt16(myDataRecord.GetOrdinal("EnquiryType"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myEnquiryTypeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myEnquiryTypeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myEnquiryTypeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myEnquiryTypeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myEnquiryTypeTable;
        }
    }

}
